import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import 'package:study_flutter/yzhg/project/shop/model/GoodsDetailBean.dart';
import 'package:study_flutter/yzhg/project/shop/provide/GoodsDetailProvide.dart';

class DetailCommentWidget extends StatefulWidget {
  @override
  _DetailCommentWidgetState createState() => _DetailCommentWidgetState();
}

class _DetailCommentWidgetState extends State<DetailCommentWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    GoodInfo goodInfo = Provide.value<GoodsDetailProvide>(context).goodInfo;
    List<GoodComments> goodComments =
        Provide.value<GoodsDetailProvide>(context).goodComments;
    print("=============================222");
    goodComments.forEach((element) {
      print(element.userName);
    });
    return Provide<GoodsDetailProvide>(builder: (context, child, childData) {
      if (childData.detailCommentStatus) {
        return Container(
          margin: EdgeInsets.only(bottom: ScreenUtil().setHeight(80)),
          width: ScreenUtil().setWidth(750),
          child: Html(data: goodInfo.goodsDetail),
        );
      } else {
        if (goodComments != null && goodComments.length != 0) {
          return Container(
            margin: EdgeInsets.only(bottom: ScreenUtil().setHeight(80)),
            child: ListView.builder(
                shrinkWrap: true, //解决无限高度问题
                physics: new NeverScrollableScrollPhysics(), //禁用滑动事件
                itemCount: goodComments.length,
                itemBuilder: (context, index) {
                  // return Text(goodComments[index].userName);
                  return _CommentWidget(goodComments[index]);
                }),
          );
        } else {
          return Container(
            alignment: Alignment.topCenter,
            padding: EdgeInsets.all(24.0),
            child: Text(
              "暂时没有评价",
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(28.0), color: Colors.black),
            ),
          );
        }
      }
    });
  }

  /*评论  数据*/
  Widget _CommentWidget(GoodComments goodComments) {
    return Container(
      width: ScreenUtil().setWidth(750),
      margin: EdgeInsets.only(top: 4.0),
      padding: EdgeInsets.only(left: 24.0, top: 12.0, bottom: 12.0),
      decoration: BoxDecoration(color: Colors.white),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                "昵称:${goodComments.userName}",
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(26.0), color: Colors.black),
              ),
              Padding(
                padding: EdgeInsets.only(left: 24.0),
                child: Text(
                  formatDate(
                      DateTime.fromMillisecondsSinceEpoch(
                          goodComments.discussTime),
                      [yyyy, '-', mm, '-', dd, ' ', HH, ':', mm, ':', ss]),
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(24.0),
                      color: Colors.black38),
                ),
              )
            ],
          ),
          Padding(
            padding: EdgeInsets.only(top: 24.0),
            child: Text(
              goodComments.comments,
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(28.0), color: Colors.black38),
            ),
          )
        ],
      ),
    );
  }
}
